package com.kool.system.action.counter;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.core.base.IBaseAction;
import com.kool.core.exception.AppException;
import com.kool.core.util.BeanUtils;
import com.kool.core.util.GlobalDefineUtils;
import com.kool.system.bean.SyCounterBean;
import com.kool.system.bean.io.counter.SYCNTQRYXBean;
import com.kool.system.bean.io.counter.SYCNTINFYBean;
import com.kool.system.service.CounterService;

/**
 * @DESCRIBE 查询
 */
@Controller
public class QueryCounterAction implements IBaseAction {
	@Autowired
	private CounterService service;

	@Override
	@ResponseBody
	@RequestMapping(value = "/sys/counter/queryCounter", method = RequestMethod.POST)
	public JSONObject doAction(@RequestBody String sInput) throws AppException {
		JSONObject pkgIn = JSONObject.parseObject(sInput);
		JSONArray secIn = pkgIn.getJSONArray("SYCNTQRYX");
		JSONObject propIn = secIn.getJSONObject(0);
		SYCNTQRYXBean ioIn = JSON.toJavaObject(propIn, SYCNTQRYXBean.class);

		SyCounterBean beanIn = (SyCounterBean) BeanUtils.io2Bean(ioIn, SyCounterBean.class, "sco");

		List<SyCounterBean> list = service.query(beanIn);

		JSONObject pkgOut = new JSONObject();
		JSONArray secOut = new JSONArray();
		for (SyCounterBean syCounter : list) {
			SYCNTINFYBean ioOut = (SYCNTINFYBean) BeanUtils.bean2Io(syCounter, SYCNTINFYBean.class);
			JSONObject propOut = (JSONObject) JSON.toJSON(ioOut);
			GlobalDefineUtils.addTransformation(propOut, "over", "SCO_OVER");
			GlobalDefineUtils.addTransformation(propOut, "resetPeriod", "SCO_RESET_PERIOD");
			GlobalDefineUtils.addTransformation(propOut, "state", "SCO_STATE");
			secOut.add(propOut);
		}
		pkgOut.put("SYCNTINFY", secOut);
		return pkgOut;
	}
}
